package com.qubi.exchange.rest;

import com.qubi.exchange.biz.ColaExchangeBiz;
import com.qubi.exchange.data.BalanceChange;
import com.qubi.exchange.security.auth.client.annotation.IgnoreClientToken;
import com.qubi.exchange.security.common.exception.auth.UserTokenException;
import com.qubi.exchange.security.common.msg.AppResponse;
import com.qubi.exchange.service.BalanceService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

@Log4j2
@RestController
@IgnoreClientToken
@RequestMapping("/rpc/v1")
public class RPCController {
    ExecutorService executorService = Executors.newFixedThreadPool(20);
    @Autowired
    BalanceService balanceService;

    @Value("${auth.sign}")
    String sign;


    @Autowired
    ColaExchangeBiz exchangeBiz;



    @RequestMapping("coinPrice")
    public AppResponse coinPrice(@RequestBody List<String> coins) {
        return AppResponse.ok().data(coins.stream().map(x -> exchangeBiz.getCoinPrice(x)));


    }


    @RequestMapping("balance")
    public AppResponse balance(String userId, String coinCode, String sign) {
        if (sign.equals(this.sign)) {
            var balance = balanceService.getUserBalance(userId, coinCode);
            if (balance == null) balance = balanceService.initBalanceByCoinCode(userId, coinCode);
            balance.setBalanceAvailable(balance.getBalanceAvailable().setScale(2, RoundingMode.DOWN));
            balance.setBalanceFrozen(balance.getBalanceFrozen().setScale(2, RoundingMode.DOWN));

            return AppResponse.ok().data(balance);
        }
        throw new UserTokenException("密码错误");
    }

    @RequestMapping("coinBalance")
    public AppResponse coinBalance(String coinCode, String sign) {
        if (sign.equals(this.sign)) {
            var balance = balanceService.getCoinAllBalance(coinCode);
            return AppResponse.ok().data(balance);
        }
        throw new UserTokenException("密码错误");
    }

    @RequestMapping("allBalanceWorth")
    public AppResponse allBalanceWorth(String sign) {

        if (sign.equals(this.sign)) {
            return AppResponse.ok().data(balanceService.getAllUserWorth());
        }
        throw new UserTokenException("密码错误");

    }

    @RequestMapping("changeUserBalanceSync")
    public AppResponse changeUserwBalance(@RequestBody List<BalanceChange> balanceChanges, String sign, String type) {
        if (sign.equals(this.sign)) {
            for (BalanceChange change : balanceChanges) {
                balanceService.changeUserBalance(change.getUserId(), change.getCoinCode(), change.getAvailable(), change.getFrozen(), change.getLocks());
            }

        }
        return AppResponse.ok();
    }

    @RequestMapping("changeUserBalanceAsync")

    public AppResponse changeUserBalanceAsync(@RequestBody List<BalanceChange> balanceChanges, String sign, String type) {
        if (sign.equals(this.sign)) {
            executorService.submit(() -> {
                for (BalanceChange change : balanceChanges) {
                    balanceService.changeUserBalance(change.getUserId(), change.getCoinCode(), change.getAvailable(), change.getFrozen(), change.getLocks());
                }
            });
        }
        return AppResponse.ok();
    }

    @RequestMapping("changeUserAvailable")
    public AppResponse changeUserAvailable(String userId, BigDecimal amount, String type, String coinCode) {

        return AppResponse.ok().data(balanceService.changeUserBalance(userId, coinCode, amount, BigDecimal.ZERO, BigDecimal.ZERO));
    }

}
